package com.example.park2212.park



import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import androidx.paging.cachedIn
import com.example.lib_base.entity.ParkBean
import com.example.lib_base.entity.ParkBeanItem

import com.example.lib_base.mvvm.BaseViewModel
import com.example.lib_base.net.ApiService
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject

@HiltViewModel
class ParkViewModel @Inject constructor(val apiService: ApiService):BaseViewModel(){

    fun getParksByType(para:String,type:String): Flow<PagingData<ParkBeanItem>> {
        return Pager(config = PagingConfig(pageSize = 10), pagingSourceFactory = {
            ParkDataSource(apiService,para,type)
        }).flow.cachedIn(viewModelScope)
    }

    val addPark = MutableLiveData<ParkBean>()
    fun addPark(parkBean: ParkBeanItem){
        httpRequest({
            apiService.addPark(mapToBody(parkBean))
        },{addPark.value = it})
    }

    val updateParkSuccess = MutableLiveData<ParkBeanItem>()
    fun updatePark(id:Int,beanItem:ParkBeanItem){
        httpRequest({
            apiService.updatePark(id,mapToBody(beanItem))
        },updateParkSuccess::postValue)
    }
}